<?php
date_default_timezone_set('Asia/Shanghai');
set_time_limit(0);
ignore_user_abort(TRUE);

require_once './vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
define('IN_ECS', true);

require(dirname(__FILE__) . '/cls_mysql.php');

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "AC7eeee8029a31d4c86ebb0b6c2728c86e";
$token = "d6badaeebdeb7b5d6474887bd25e3ef2";
$client = new Client($sid, $token);

$db = new cls_mysql("34.208.3.243", "kuaisong_api1", "FZP5bw6XRCDtXzHs","kuaisong","utf8");

 $db1 = new cls_mysql("localhost:3306", "dscmall", "ETDCwmkd7X","dscmall","utf8");

//$sql = 'select * from tblsendmms where nickname = "Fearlessの黑郁金香"';

//$sql = 'select  DISTINCT mobile from tblsendmms';
 $sql = "SELECT distinct mobile  FROM `ecs_order_info` WHERE shop_id in (SELECT  cat_id  FROM `ecs_category` WHERE shop_area=3) and pay_status=2 and order_status<>2 and shipping_status=2";

$all1 = $db->getAll($sql);
 
//$file = fopen("phone_nums.txt", "r");
//$all1 = array();
//$i = 0;
//while (!feof($file)) {
//    $all1[]['mobile'] = fgets($file);
//}
//fclose($file);

//$all = array(
//    array('mobile'=>'7787516669'),
//    array('mobile'=>'6043642383')
//);

$all2 = $db1->getAll("select mobile from ecs_sms_send_log");
$all_m = array();

foreach($all2 as $k=>$v){
    $all_m[]=$v['mobile'];
}

$all = array();

foreach($all1 as $k=>$v){
    if(array_search($v['mobile'], $all_m)===FALSE){
        $all[]=$v;
    }
}

//echo count($all);die();
//$all= array();
//$all[] = array('mobile'=>'6043642383');

$suc = 0;
$fail = 0;
echo "start:".date("Y-m-d H:i:s",time())."\n";

foreach($all as $k=>$v){
//    if($v['is_vip']){
//        $msg = '亲爱的'.$v['nickname'].', 大温快送祝您圣诞快乐！ 新年将至，我们会有更棒的服务、更多的店家、更新的创意等着您来体验！2018 小黄帽与您不见不散！';
//    }else{
//       $msg = '尊敬的客户，您好！感谢您使用大温快送平台！我们在微信商城给您准备了新用户红包，快去我的优惠券查看吧！';
       $msg = "";
//    }
    try {
        $rs = $client->messages
        ->create(
            '+1'.$v['mobile'],
            array(
                "from" => "+17786558663",
                "body" => $msg,
                "mediaUrl" => "https://staging.quitesoondelivery.com/mobile/themes/ecmoban_baiduMall/images/ads/pindan_sms.jpeg"
            )
        );
        $suc++;
        usleep(100);
        
        $arr = array(
            'mobile'=>$v['mobile'],
            'status'=>1,
            'add_time'=>date("Y-m-d H:i:s",time())
        );
        $db1->autoExecute('ecs_sms_send_log', $arr);
        
    } catch (Exception $e) {
        print "The ".$k." number fail:";
        $fail++;
        print $e->getMessage()."\n";
        
        $arr = array(
            'mobile'=>$v['mobile'],
            'status'=>0,
            'add_time'=>date("Y-m-d H:i:s",time())
        );
        $db1->autoExecute('ecs_sms_send_log', $arr);
    }
    
}
echo "fail:$fail,success:$suc";
echo "end:".date("Y-m-d H:i:s",time())."\n";

 